#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "fn_addEvent.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_event_name","_locality","_handler","_event_unique_name"];
DEFAULT_PARAM(_event_name,0,"");
DEFAULT_PARAM(_locality,1,0);
DEFAULT_PARAM(_handler,2,{});

if (!IS_STRING(_event_name) || {_event_name == "") exitWith { ERROR_1("Invalid Event Name",_event_name); };
if (!IS_SCALAR(_locality) && !IS_STRING(_locality)) exitWith { ERROR_1("Invalid Locality",_locality); };

if (IS_SCALAR(_locality)) then { _locality = str(_locality) };
_locality = switch(toUpper(_locality)) do {
	case "0": {0};
	case "1": {1};
	case "2": {2};
	case "ALL": {0};
	case "TOUS": {0};
	case "BOTH": {0};
	case "SERVEUR": {1};
	case "SERVER": {1};
	case "CLIENT": {2};
	case "JOUEUR": {2};
	default {0};
};

if (!isnull GVAR(ObjectStore)) then {
	if (_locality == 0 || {_locality == 1 && isServer} || {_locality == 2 && !isDedicated}) then {
		_event_unique_name = format["%1_%2",QGVAR(event),toUpper(_event_name)];
		TRACE_1("Adding Event",_event_name);
		GVAR(ObjectStore) setVariable [_event_unique_name,_handler];
	};
} else {
	ERROR("ObjectStore does not exist");
};

TRACE("End");
TMFPROFILERSTOP;
